home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 10 - 1994 / 10.04 Apr 94 / Powering Up / Executing Code / PictViewer.c < prev    next >
Encoding:
Text File  |  1994-02-09  |  1.9 KB  |  85 lines  |  [TEXT/MPCC]

  1. // File: PictViewer.c
  2. //
  3. // This is a piece of code which can be loaded and called by SimpleApp
  4.  
  5. #include <Quickdraw.h>
  6. #include <Windows.h>
  7. #include <Files.h>
  8. #include <Errors.h>
  9. #include <Events.h>
  10.  
  11. #include <FragLoad.h>
  12.  
  13. // The following constants are copied from CodeFragmentTypes.r
  14. // They *should* be in FragLoad.h, but aren't
  15. enum {
  16.         kInMem = 0,
  17.         kOnDiskFlat,
  18.         kOnDiskSegmented
  19.     };
  20.  
  21. // === Global variables
  22. PicHandle    gOurPicture = NULL;
  23.  
  24. // The Initialization routine has a standard calling sequence, as defined in FragLoad.h
  25. OSErr OurInitRoutine (InitBlockPtr initBlkPtr)
  26. {
  27.     OSErr    err = noErr;
  28.     short    refNum = -1;
  29.     
  30.     // Make sure this code is coming from the data fork of a file
  31.     if (initBlkPtr->fragLocator.where != kOnDiskFlat) {
  32.         err = fnfErr;    // We didn't come from a file (I'd call that "file not found!" :))
  33.         goto done;
  34.     }
  35.         
  36.     refNum = FSpOpenResFile(initBlkPtr->fragLocator.u.onDisk.fileSpec,
  37.                             fsCurPerm);
  38.     if (refNum == -1) {
  39.         err = ResError();
  40.         goto done;
  41.     }
  42.     
  43.     UseResFile(refNum);
  44.     gOurPicture = (PicHandle)GetIndResource('PICT', 1);
  45.     if (gOurPicture)    
  46.         DetachResource((Handle)gOurPicture);
  47.     else
  48.         err = ResError();
  49.  
  50. done:
  51.     if (refNum != -1)
  52.         CloseResFile(refNum);
  53.     return err;
  54. }
  55.  
  56.  
  57. // Our main routine can have any calling sequence we want
  58. // This particular version is defined in SimpleApp.c
  59. Boolean OurMainRoutine (EventRecord *theEvent, WindowPtr currWindow, QDGlobals *qdAddress)
  60. {
  61.     Boolean    handled = false;
  62.     
  63.     if (theEvent->what == updateEvt) {
  64.         WindowPtr    theWindow = (WindowPtr)theEvent->message;
  65.         
  66.         BeginUpdate(theWindow);
  67.         SetPort(theWindow);
  68.         DrawPicture(gOurPicture, &currWindow->portRect);
  69.         EndUpdate(theWindow);
  70.         handled = true;
  71.     }
  72.     
  73.     return handled;    // Did we handle the event?
  74. }
  75.  
  76.  
  77. void OurTerminationRoutine()
  78. {
  79.     // This will be called when this code fragment is about to be unloaded
  80.     
  81.     // Release the memory used for our picture
  82.     if (gOurPicture != NULL)
  83.         KillPicture(gOurPicture);
  84. }
  85.